﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ebc.componentbinder;
using ebc.componentbinder.Contract;
using kp.compiler;
using kp.compiler.contract;
using kp.koordinator;
using kp.koordinator.contract;
using kp.plotter;
using kp.plotter.contract;
using kp.portal.contract;
using kp.rechenwerk;
using kp.rechenwerk.contract;

namespace kp.host
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Build
            IComponentBinder binder = new ComponentBinder();

            ICompiler compiler = new Compiler();
            IRechenwerk rechenwerk = new Rechenwerk();
            IKoordinator koordinator = new Koordinator();
            IPlotter plotter = new kp.plotter.Plotter();
            IPortal portal = new Portal((UserControl)plotter);

            // Bind
            //koordinator.OnCompilationRequest += 
            //    req =>
            //        {
            //            File.AppendAllText("kp.host.log", "compiling: " + req.formel + "\r\n");
            //            compiler.ProcessCompilationRequest(req);
            //        };
            //koordinator.OnBerechnungsauftrag += rechenwerk.ProcessBerechnungsauftrag;

            //portal.OnBerechnungsauftrag += 
            //    req => {
            //               var originalResultProcessor = req.processResult;
            //               req.processResult = result =>
            //                                       {
            //                                           File.AppendAllText("kp.host.log", "anzahl berechneter werte: " + result.Count() + "\r\n");
            //                                           originalResultProcessor(result);
            //                                       };
            //               koordinator.ProcessBerechnungsauftrag(req);
            //           };
            //portal.OnShowPlotCommand += plotter.ProcessShowPlotCommand;
            //portal.OnWerteanzahlAnfrage += plotter.ProcessWerteanzahlAnfrage;

            binder.Bind(new object[] {compiler, rechenwerk, koordinator, plotter, portal},
                        LogMessage);

            // Run
            Application.Run((Form)portal);
        }


        static void LogMessage(object message)
        {
            File.AppendAllText("kp.host.log", string.Format("{0}: {1}\r\n", DateTime.Now, message));
        }
    }
}
